import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import lodash from 'lodash';

import * as util from './utils/util';

window._ = lodash;
window.U = util;
window.obj = util.obj;
window.lodash = lodash;

Vue.use(ElementUI, { size: 'small', zIndex: 3000 });

Vue.prototype.msgSuccess = function (msg = '成功', tip) {
  tip && console.log(`%c${msg} : ${tip}`, 'color: green');
  this.$message({ showClose: true, message: msg, type: 'success' });
};

Vue.prototype.msgError = function (msg = '错误', err, tip) {
  msg && console.error(msg);
  err && console.error(err);
  tip && console.error(tip);
  this.$message({ showClose: true, message: msg, type: 'error' });
};

Vue.prototype.msgInfo = function (msg = '提示') {
  this.$message.info(msg);
};

// this.__loading(); this.__loading('加载中', 5000);
Vue.prototype.__loading = function (text = '', autoClose = false) {
  const loading = this.$loading({
    lock: !true, // 是否锁屏
    text, // 加载动画的文字
    spinner: 'el-icon-loading', // 引入的loading图标
    background: 'rgba(0, 0, 0, 0.3)', // 背景颜色
    target: '.sub-main', // 需要遮罩的区域
    body: true,
    customClass: 'mask' // 遮罩层新增类名
  });
  if (autoClose) {
    setTimeout(function () {
      loading.close(); // 关闭遮罩层
    }, autoClose);
  }
  return loading;
};

Vue.config.errorHandler = function (err, vm, info) {
  Vue.nextTick(() => {
    if (process.env.NODE_ENV === 'development') {
      console.group('>>>>>> 错误信息 >>>>>>');
      console.log(info);
      console.groupEnd();
      console.group('>>>>>> Vue 实例 >>>>>>');
      console.log(vm);
      console.groupEnd();
      console.group('>>>>>> Error >>>>>>');
      console.log(err);
      console.groupEnd();
    }
  });
};
